<? /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: t_order Model
	+ Description	: For record model process back-end
	+ Filename 		: c_t_order.php
 	+ Author  		: 
 	+ Created on 05/Mar/2012 14:57:56

*/

class M_t_order extends CI_Model{

		//constructor
		function M_t_order() {
			parent::__construct();
		}

		//function for detail
		//get record list
		function detail_t_orderdet_list($master_id,$query,$start,$end) {

			$sql = "SELECT orderdet_id,
							t_order_id,
							orderdet_detail,
							orderdet_qty,
							orderdet_price					  
					FROM   t_orderdet
					WHERE  t_order_id='".$master_id."'";

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}
		//end of function


		//insert detail record
		function detail_t_orderdet_insert($array_orderdet_id,
						$t_order_id,
						$array_orderdet_detail,
						$array_orderdet_qty,
						$array_orderdet_price){

			if($t_order_id==0){
				return '0';
			}else{
				$query="";
				for($i = 0; $i < sizeof($array_orderdet_id); $i++){
					$data = array(
						"orderdet_id"=>$array_orderdet_id[$i],
						"t_order_id"=>$t_order_id,
						"orderdet_detail"=>$array_orderdet_detail[$i],
						"orderdet_qty"=>$array_orderdet_qty[$i],
						"orderdet_price"=>$array_orderdet_price[$i]					);

					if($array_orderdet_id[$i]==0){
						$this->db->insert('t_orderdet', $data);

						$query = $query.$this->db->insert_id();
						if($i<sizeof($array_orderdet_id)-1){
							$query = $query . ",";
						}

					}else{
						$query = $query.$array_orderdet_id[$i];
						if($i<sizeof($array_orderdet_id)-1){
							$query = $query . ",";
						}
						$this->db->where('orderdet_id', $array_orderdet_id[$i]);
						$this->db->update('t_orderdet', $data);
					}
				}

				if($query<>""){
					$sql="DELETE FROM t_orderdet WHERE  t_order_id='".$t_order_id."' AND
						orderdet_id NOT IN (".$query.")";
					$this->db->query($sql);
				}

				return '1';
			}
		}
		//end of function

		//function for get list record
		function t_order_list($filter,$start,$end){
			$sql = "SELECT t_order.order_id,
                            t_order.order_code,
                            t_order.order_date,
                            t_order.order_duedate,
                            m_customer.customer_name,
                            m_staff.staff_name,
                            t_order.order_stat,
                            t_order.order_note
                            FROM t_order
                            LEFT JOIN m_customer on (m_customer.customer_id = t_order.m_customer_id)
                            LEFT JOIN m_staff on (m_staff.staff_id = t_order.m_staff_id)";

			// For simple search
			if ($filter<>""){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(order_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(order_code) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(order_date) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(order_duedate) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(m_customer_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(m_staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(order_stat) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(order_note) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;	
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function t_order_create($order_code,
				 $order_date,
				 $order_duedate,
				 $m_customer_id,
				 $m_staff_id,
				 $order_stat,
				 $order_note){
			$data = array(
						 "order_code"=>$order_code,
						 "order_date"=>$order_date,
						 "order_duedate"=>$order_duedate,
						 "m_customer_id"=>$m_customer_id,
						 "m_staff_id"=>$m_staff_id,
						 "order_stat"=>$order_stat,
						 "order_note"=>$order_note					);
			$this->db->insert('t_order', $data);
			if($this->db->affected_rows())
				return $this->db->insert_id();
			else
				return '0';
		}

		//function for update record
		function t_order_update($order_id,
                                 $order_code,
				 $order_date,
				 $order_duedate,
				 $m_customer_id,
				 $m_staff_id,
                                 $order_stat,
				 $order_note){
			$data = array(
						"order_date"=>$order_date,
						"order_duedate"=>$order_duedate,
						"order_note"=>$order_note					);
                        
                        if(is_numeric($m_customer_id)){
                                $this->firephp->log("cust_id : ".$m_customer_id);
				$sql_customer = "SELECT customer_id FROM m_customer WHERE customer_id=".$m_customer_id;
				$rs_customer = $this->db->query($sql_customer);
				if($rs_customer->num_rows()){
					$row = $rs_customer->row();
					$data['m_customer_id'] = $row->customer_id;
				}
			}
                       
			if(is_numeric($m_staff_id)){
				$sql_staff = "SELECT staff_id FROM m_staff WHERE staff_id=".$m_staff_id;
                                $this->firephp->log("staff_id : ".$m_staff_id);
				$rs_staff = $this->db->query($sql_staff);
				if($rs_staff->num_rows()){
					$row = $rs_staff->row();
					$data['m_staff_id'] = $row->staff_id;
				}
			}                        

			$this->db->where('order_id', $order_id);
			$this->db->update('t_order', $data);
			return $order_id;
		}

		//fcuntion for delete record
		function t_order_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the t_orders at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM t_order WHERE order_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM t_order WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "order_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function t_order_search($order_id,
				 $order_code,
				 $order_date,
				 $order_duedate,
				 $m_customer_id,
				 $m_staff_id,
				 $order_stat,
				 $order_note,$start,$end){
			//full query
			$sql = "SELECT order_id,
							 order_code,
							 order_date,
							 order_duedate,
							 m_customer_id,
							 m_staff_id,
							 order_stat,
							 order_note					  
					FROM   t_order";

			if($order_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_id) LIKE '%".addslashes(strtolower($order_id))."%'";
			};
			if($order_code!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_code) LIKE '%".addslashes(strtolower($order_code))."%'";
			};
			if($order_date!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_date) LIKE '%".addslashes(strtolower($order_date))."%'";
			};
			if($order_duedate!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_duedate) LIKE '%".addslashes(strtolower($order_duedate))."%'";
			};
			if($m_customer_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_customer_id) LIKE '%".addslashes(strtolower($m_customer_id))."%'";
			};
			if($m_staff_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
			};
			if($order_stat!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_stat) LIKE '%".addslashes(strtolower($order_stat))."%'";
			};
			if($order_note!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(order_note) LIKE '%".addslashes(strtolower($order_note))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
                        $limit = $sql." LIMIT ".$start.",".$end;	
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function t_order_print($order_id,
				 $order_code,
				 $order_date,
				 $order_duedate,
				 $m_customer_id,
				 $m_staff_id,
				 $order_stat,
				 $order_note,$option,$filter){
			//full query
			$sql = "SELECT order_id,
							 order_code,
							 order_date,
							 order_duedate,
							 m_customer_id,
							 m_staff_id,
							 order_stat,
							 order_note					  
					FROM   t_order";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(order_id) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(order_code) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(order_date) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(order_duedate) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(m_customer_id) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(m_staff_id) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(order_stat) LIKE '%".addslashes(strtoloer($filter))."%' OR 
				          lower(order_note) LIKE '%".addslashes(strtoloer($filter))."%' )";
			} else if($option=='SEARCH'){
				if($order_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_id) LIKE '%".addslashes(strtolower($order_id))."%'";
				};
				if($order_code!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_code) LIKE '%".addslashes(strtolower($order_code))."%'";
				};
				if($order_date!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_date) LIKE '%".addslashes(strtolower($order_date))."%'";
				};
				if($order_duedate!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_duedate) LIKE '%".addslashes(strtolower($order_duedate))."%'";
				};
				if($m_customer_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_customer_id) LIKE '%".addslashes(strtolower($m_customer_id))."%'";
				};
				if($m_staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
				};
				if($order_stat!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_stat) LIKE '%".addslashes(strtolower($order_stat))."%'";
				};
				if($order_note!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_note) LIKE '%".addslashes(strtolower($order_note))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function t_order_export_excel($order_id ,$order_code ,$order_date ,$order_duedate ,$m_customer_id ,$m_staff_id ,$order_stat ,$order_note ,$option,$filter){
			//full query
			$sql = "SELECT order_id,
						   order_code,
						   order_date,
						   order_duedate,
						   m_customer_id,
						   m_staff_id,
						   order_stat,
						   order_note					  
					FROM   t_order";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(order_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(order_code) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(order_date) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(order_duedate) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_customer_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(order_stat) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(order_note) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($order_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_id) LIKE '%".addslashes(strtolower($order_id))."%'";
				};
				if($order_code!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_code) LIKE '%".addslashes(strtolower($order_code))."%'";
				};
				if($order_date!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_date) LIKE '%".addslashes(strtolower($order_date))."%'";
				};
				if($order_duedate!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_duedate) LIKE '%".addslashes(strtolower($order_duedate))."%'";
				};
				if($m_customer_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_customer_id) LIKE '%".addslashes(strtolower($m_customer_id))."%'";
				};
				if($m_staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
				};
				if($order_stat!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_stat) LIKE '%".addslashes(strtolower($order_stat))."%'";
				};
				if($order_note!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(order_note) LIKE '%".addslashes(strtolower($order_note))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query;
		}


}
?>